/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.unindra.company.dao.impl;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import edu.unindra.company.dao.entity.User;
import edu.unindra.company.dao.intrf.UserInterf;
import edu.unindra.company.dao.koneksi.DatabaseUtilities;

/**
 * 
 * @author fendi
 */
public class UserImpl implements UserInterf {

	public void insert(User user) throws SQLException {
		PreparedStatement ps = null;
		try {
			ps = DatabaseUtilities.getInstance().getConnection()
					.prepareStatement("insert into user values (?,?)");

			ps.setString(1, user.getNama());
			ps.setString(2, user.getPassword());
			ps.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			ps.close();
		}

	}

	public User getUser() throws SQLException {
		Statement st = null;
		ResultSet rs = null;
		User us = null;

		try {
			st = DatabaseUtilities.getInstance().getConnection()
					.createStatement();

			rs = st.executeQuery("select * from user");
			us = new User();
			while (rs.next()) {
				us.setNama(rs.getString("nama"));
				us.setPassword(rs.getString("password"));
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			st.close();
			rs.close();
		}

		return us;
	}

}
